package ar.com.mindset {
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;


	public class MindsetCustomPreloader extends PreloaderDisplayBase {

		[Embed("/assets/logo-geometrics-preloader.png") ]
		[Bindable]
		public var Logo:Class;

		private var t:TextField;
		private var t2:TextField;
		private var tvideo:TextField;
		private var f:DropShadowFilter=new DropShadowFilter(2, 45, 0x000000, 0.5)
		private var pathfLogo:DisplayObject;
		private var bar:Sprite=new Sprite();
		private var barFrame:Sprite;
		private var mainColor:uint=0xFFFFFF;
		
		private var timer:Timer
		
		private var datosProgress:Number = 0;   // 0-1

		private var rellenoBarra:uint=0x00C0F3;//FF8800;
		private var bordeBarra:uint=0x0072AE;
		private var colorTexto:uint=0x00C0F3;
		private var barHeight:int = 5;
		private var radius:int = 10;
		

		public function MindsetCustomPreloader() {
			super();
		}

		// This is called when the preloader has been created as a child on the stage.
		//  Put all real initialization here.
		override public function initialize():void {
			super.initialize();

			clear(); // clear here, rather than in draw(), to speed up the drawing
			var indent:int=20;
			var height:int=20;

			//creates all visual elements
			createAssets();
		}

		//this is our "animation" bit
		override protected function draw():void {
			t.text=int(_fractionLoaded * 100).toString() + "%";
			var d:Number=barFrame.x + barFrame.width * _fractionLoaded
			t.x=d - t.width - 25;
			//make objects below follow loading progress
			//positions are completely arbitrary
			//d tells us the x value of where the loading bar is at


			//t2.x=t.x - 50
			pathfLogo.x=d - pathfLogo.width * 0.8;
			bar.graphics.beginFill(rellenoBarra, 1)
			bar.graphics.drawRoundRectComplex(0, 0, bar.width * _fractionLoaded, barHeight, radius, radius, radius, radius);
			bar.graphics.endFill();
		}

		protected function createAssets():void {
			//this.backgroundColor=0x000000;
			//this.backgroundAlpha=1;

			//craate bar
			bar=new Sprite();
			bar.graphics.drawRoundRectComplex(0, 0, 200, barHeight, radius, radius, radius, radius);
			bar.x=stageWidth / 2 - bar.width / 2;
			bar.y=stageHeight / 2 - bar.height / 2;
			bar.filters=[f];
			addChild(bar);

			//create bar frame
			barFrame=new Sprite();
			barFrame.graphics.lineStyle(1, bordeBarra, 1)
			barFrame.graphics.drawRoundRectComplex(0, 0, 200, barHeight, radius, radius, radius, radius);
			barFrame.graphics.endFill();
			barFrame.x=stageWidth / 2 - barFrame.width / 2;
			barFrame.y=stageHeight / 2 - barFrame.height / 2;
			barFrame.filters=[f];
			addChild(barFrame);


			//create the logo
			pathfLogo=new Logo();
			pathfLogo.y=stageHeight / 2 - pathfLogo.height * 1.5;
			//pathfLogo.x=barFrame.x + barFrame.width / 2 - pathfLogo.width / 2;

			//pathfLogo.y=barFrame.y - pathfLogo.height - 40;

			pathfLogo.filters=[f];
			addChild(pathfLogo);

			//create text field to show percentage of loading
			t=new TextField()
			t.y=barFrame.y - 35;
			t.filters=[f];
			//t.width=barFrame.width;
			addChild(t);

			//create text field to show percentage of loading
			/*t2=new TextField()
			t2.text="nada";
			t2.y=barFrame.y - 80;
			t2.filters=[f];
			addChild(t2);*/


			//we can format our text
			var s:TextFormat=new TextFormat("Verdana", null, colorTexto, null, null, null, null, null, "right");
			t.defaultTextFormat=s;

			//create text field to show percentage of loading
			/*tvideo=new TextField()
			tvideo.y=barFrame.y - 80;
			tvideo.filters=[f];
			addChild(tvideo);
			//we can format our text
			tvideo.defaultTextFormat=s;*/
		}

		protected function clear():void {
			// Draw gradient background
			var b:Sprite=new Sprite;
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(stageWidth, stageHeight, Math.PI / 2);
			b.graphics.beginGradientFill(GradientType.LINEAR,
				[0x000000, mainColor],             
				//[0x000000, 0x000000], 
				[1, 1], [0, 255], matrix);
			b.graphics.drawRect(0, 0, stageWidth, stageHeight);
			b.graphics.endFill();
			addChild(b);
		}
	}
}